<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="客户名称" prop="kname">
        <el-input
          v-model="queryParams.kname"
          placeholder="请输入客户名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="车牌号" prop="carno">
        <el-input
          v-model="queryParams.carno"
          placeholder="请输入车牌号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="车型" prop="chexing">
        <el-input
          v-model="queryParams.chexing"
          placeholder="请输入车型"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="车架号" prop="chejiahao">
        <el-input
          v-model="queryParams.chejiahao"
          placeholder="请输入车架号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="备注" prop="beizhu">
        <el-input
          v-model="queryParams.beizhu"
          placeholder="请输入备注"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="Refresh" @click="resetQuery" v-if="!props.isSelect"
          >重置</el-button
        >
        <el-button type="danger" v-if="props.isSelect" @click="add_select"
          >新增</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8" v-if="!props.isSelect">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['carrepair:car:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['carrepair:car:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['carrepair:car:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['carrepair:car:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="carList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="客户名称" align="center" prop="kname" />
      <el-table-column label="电话" align="center" prop="dianhua" />
      <el-table-column label="车牌号" align="center" prop="carno" />
      <el-table-column label="车型" align="center" prop="chexing" />
      <el-table-column label="车架号" align="center" prop="chejiahao" />
      <el-table-column label="备注" align="center" prop="beizhu" />
      <el-table-column label="保险公司" align="center" prop="bxgsName" />
      <el-table-column
        label="保险到期日期"
        align="center"
        prop="baoxiandqrq"
        width="180"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.baoxiandqrq, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button
            link
            type="primary"
            icon="Edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['carrepair:car:edit']"
            >修改</el-button
          >
          <el-button
            link
            type="primary"
            icon="Delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['carrepair:car:remove']"
            >删除</el-button
          >
          <el-button
            v-if="props.isSelect"
            link
            type="danger"
            @click="selectRow(scope.row)"
            >选择</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改车辆管理对话框 -->
    <el-dialog :title="title" v-model="open" width="90%" append-to-body>
      <el-form
        ref="carRef"
        :model="form"
        :rules="rules"
        label-width="80px"
        style="height: 70vh"
      >
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="客户名称" prop="kname">
              <el-input v-model="form.kname" placeholder="请输入客户名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="电话" prop="dianhua">
              <el-input v-model="form.dianhua" placeholder="请输入电话" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="车牌号" prop="carno">
              <el-input v-model="form.carno" placeholder="请输入车牌号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="车型" prop="chexing">
              <el-input v-model="form.chexing" placeholder="请输入车型" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="车架号" prop="chejiahao">
              <el-input v-model="form.chejiahao" placeholder="请输入车架号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="保险公司" prop="bxgsName">
              <el-input
                v-model="form.bxgsName"
                placeholder="请输入保险公司"
                readonly
                style="width: calc(100% - 80px)"
              />
              <el-button type="primary" style="width: 80px" @click="selectBxgs"
                >选择</el-button
              >
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item
              label="保险到期日期"
              prop="baoxiandqrq"
              label-width="100px"
            >
              <el-date-picker
                clearable
                v-model="form.baoxiandqrq"
                type="date"
                value-format="YYYY-MM-DD"
                placeholder="请选择保险到期日期"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12"> </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="24">
            <el-form-item label="备注" prop="beizhu">
              <el-input v-model="form.beizhu" placeholder="请输入备注" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>

    <el-dialog
      title="保险公司设置"
      v-model="openBxgs"
      width="90%"
      append-to-body
    >
      <bxgsListPage
        :isSelect="true"
        @checkRow="setBxgsItem"
        @closeWin="closeWin"
      ></bxgsListPage>
    </el-dialog>
  </div>
</template>

<script setup name="Car">
import { ElMessage } from "element-plus";
import {
  listCar,
  getCar,
  delCar,
  addCar,
  updateCar,
} from "@/api/carrepair/car";

import { getBxgs } from "@/api/carrepair/bxgs";

import bxgsListPage from "@/views/carrepair/bxgs/index";

const { proxy } = getCurrentInstance();

let openBxgs = ref(false);
const selectBxgs = () => {
  openBxgs.value = true;
};

const carList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    kname: null,
    carno: null,
    chexing: null,
    chejiahao: null,
    beizhu: null,
  },
  rules: {
    kname: [{ required: true, message: "客户名称不能为空！", trigger: "blur" }],
    carno: [{ required: true, message: "车牌号不能为空！", trigger: "blur" }],
  },
});

const { queryParams, form, rules } = toRefs(data);

let props = defineProps({
  isSelect: {
    type: Boolean,
    default: false,
  },
});

const closeWin = () => {
  openBxgs.value = false;
  open.value = false;
};

let emit = defineEmits(["checkRow","closeWin"]);

const selectRow = (obj) => {
  emit("checkRow", obj);
};

const add_select =()=>{
  emit("closeWin","/wxdnr/car/");
}

const setBxgsItem = (obj) => {
  console.log("obj", obj);
  data.form.bxgsName = obj.mingcheng;
  data.form.bxgsid = obj.id;
  openBxgs.value = false;
};

/** 查询车辆管理列表 */
function getList() {
  loading.value = true;
  listCar(queryParams.value).then(async (response) => {
    let list = response.rows;
    for (var i = 0; i < list.length; i++) {
      let obj = { bxgsName: "" };
      if (list[i].bxgsid != null) {
        await getBxgs(list[i].bxgsid).then((res) => {
          console.log("res", res);
          if (res.data) {
            obj = { bxgsName: res.data.mingcheng };
          }
        });
      }
      Object.assign(list[i], obj);
    }
    carList.value = list;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    kname: null,
    dianhua: null,
    carno: null,
    chexing: null,
    chejiahao: null,
    beizhu: null,
    CreateID: null,
    Creator: null,
    CreateDate: null,
    ModifyID: null,
    Modifier: null,
    ModifyDate: null,
    IsDeleted: null,
    RowVersion: null,
    bxgsid: null,
    baoxiandqrq: null,
    toApplet: null,
    bxgsName: null,
  };
  proxy.resetForm("carRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加车辆管理";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value;
  getCar(_id).then(async (response) => {
    let obj = { bxgsName: "" };
    if (response.data.bxgsid != null) {
      await getBxgs(response.data.bxgsid).then((res) => {
        console.log("res", res);
        if (res.data) {
          obj = { bxgsName: res.data.mingcheng };
        }
      });
    }
    Object.assign(obj, response.data);
    form.value = obj;
    open.value = true;
    title.value = "修改车辆管理";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["carRef"].validate((valid) => {
    if (valid) {
      if (form.value.id != null) {
        updateCar(form.value).then((response) => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addCar(form.value).then((response) => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal
    .confirm('是否确认删除车辆管理编号为"' + _ids + '"的数据项？')
    .then(function () {
      return delCar(_ids);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download(
    "carrepair/car/export",
    {
      ...queryParams.value,
    },
    `car_${new Date().getTime()}.xlsx`
  );
}

getList();
</script>
